import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { render } from "/@/utils/common/renderUtils";
import { JVxeTypes, JVxeColumn } from '/@/components/jeecg/JVxeTable/types'
import { UploadBizType } from '/@/enums/uploadEnum';
import { useUserStore } from '/@/store/modules/user';

const userStore = useUserStore()
// 定义可选择的组件类型
export enum ComponentTypes {
  Default = 'layouts/default/index',
  IFrame = 'sys/iframe/FrameBlank',
}

export const columns: BasicColumn[] = [
  // {
  //   title: '商品图片',
  //   dataIndex: 'images',
  //   slot: 'images',
  //   ifShow: (column) => {
  //     console.log('column', column)
  //     return column['images'] && column['images'] !== ''
  //   },
  //   width: 120
  // },
  {
    title: '商品名称',
    dataIndex: 'name',
    width: 200
  },
  {
    title: '多规格',
    dataIndex: 'multiSpec',
    customRender: ({text}) => {
      return render.renderTag(text ? '是' : '否', text ? 'green' : 'cyan')
    },
    width: 64,
  },
  {
    title: '商品分类',
    dataIndex: 'categoryName',
    customRender: ({text}) => {
      if (!text) {
        return ''
      }
      return render.renderTag(text.split(','), 'green')
    },
    width: 100,
  },
  {
    title: '单位',
    dataIndex: 'unit',
    width: 64,
  },
  {
    title: '销量',
    ifShow: () => userStore.getSelectTenant.isMainTenant,
    dataIndex: 'saleNumber',
    width: 100,
  },
  {
    title: '可用库存',
    ifShow: () => userStore.getSelectTenant.isMainTenant,
    dataIndex: 'saleNumber',
    width: 100,
  },
  {
    title: '状态',
    dataIndex: 'status',
    ifShow: () => userStore.getSelectTenant.isMainTenant,
    customRender: ({text}) => {
      let color = 'green'
      if (text === '2') {
        color = 'red'
      }
      return render.renderTag(render.renderDict(text, 'goods_status'), color)
    },
    width: 64,
  },
  {
    title: '商品类型',
    dataIndex: 'goodsTypeTexts',
    customRender: ({text}) => {
      let color = 'orange'
      return render.renderTag(text, color)
    },
    width: 80,
  },
  
  {
    title: '已上架门店',
    dataIndex: 'saleNumber',
    slot: 'publicStore',
    ifShow: () => userStore.getSelectTenant.isMainTenant,
    width: 100,
  },
];

export const initStockColumns : BasicColumn[] = [
  {
    title: '门店',
    dataIndex: 'name',
    width: 150,
    align: 'left'
  },
  {
    title: '库存',
    dataIndex: 'stock',
    slots: { customRender: 'stock' },
    width: 120,
  },
]

/**
 * 商品上架的门店
 */
export const goodsUpStockColumns : BasicColumn[] = [
  {
    title: '门店',
    dataIndex: 'name',
    width: 150,
    align: 'left'
  },
  {
    title: '地址',
    dataIndex: 'address',
    // slots: { customRender: 'stock' },
    width: 300,
  },
]


export const searchFormSchema: FormSchema[] = [
  {
    field: 'keyword',
    label: '',
    componentProps: {
      placeholder: '请输入商品名称/条码'
    },
    component: 'Input'
  }
];

// 搜索门店商品的表单
export const searchStoreGoodsFormSchema: FormSchema[] = [
  {
    field: 'keyword',
    label: '',
    componentProps: {
      placeholder: '请输入商品名称/条码'
    },
    component: 'Input'
  },
  {
    label: '门店',
    field: 'tenantId',
    component: 'ApiSelect',
    ifShow: ({ values, model }) => {
      return userStore.getSelectTenant.isMainTenant
    }
  },
];

export const batchSettingSkuStockFormSchema : FormSchema[] = [
  {
    field: 'price',
    label: '零售价(元)',
    defaultValue: '0.00',
    required: true,
    component: 'InputNumber',
    componentProps: {
      size: 'middle',
      style: {
        width: '90%'
      }
    }
  },
  {
    field: 'costPrice',
    label: '初始成本(元)',
    defaultValue: '0.00',
    required: true,
    component: 'InputNumber',
    componentProps: {
      size: 'middle',
      style: {
        width: '90%'
      }
    }
  },
  {
    field: 'weight',
    label: '重量(kg)',
    defaultValue: '0.00',
    required: true,
    component: 'InputNumber',
    componentProps: {
      size: 'middle',
      style: {
        width: '90%'
      }
    }
  },
  {
    field: 'stockAlarm',
    label: '库存预警',
    defaultValue: '0',
    required: true,
    component: 'InputNumber',
    componentProps: {
      size: 'middle',
      style: {
        width: '90%'
      }
    }
  },
]

export const formSchema: FormSchema[] = [
  {
    label: 'id',
    field: 'id',
    component: 'Input',
    show: false
  },
  {
    field: 'goodsNo',
    label: '商品条码',
    dynamicDisabled: !userStore.getSelectTenant.isMainTenant,
    component: 'Input'
  },
  {
    field: 'name',
    label: '商品名称',
    dynamicDisabled: !userStore.getSelectTenant.isMainTenant,
    component: 'Input',
    required: true,
  },
  {
    label: '商品图片',
    field: 'images',
    dynamicDisabled: !userStore.getSelectTenant.isMainTenant,
    component: 'MyImageUpload',
    helpMessage: '第一张图片将作为商品主图',
    componentProps: {
      fileMax: 15,
      bizType: UploadBizType.GENERAL_IMAGE
    },
  },
  {
    field: 'categoryId',
    label: '商品分类',
    dynamicDisabled: !userStore.getSelectTenant.isMainTenant,
    component: 'TreeSelect',
    required: true,
    componentProps: {
      fieldNames: {
        title: 'name',
        key: 'id',
        value: 'id',
      },
      dropdownStyle: {
        maxHeight: '50vh'
      },
      placeholder: '请选择分类',
      showSearch: true,
      getPopupContainer: (node) => node.parentNode
    }
  },
  {
    field: 'unit',
    label: '单位',
    required: true,
    dynamicDisabled: !userStore.getSelectTenant.isMainTenant,
    component: 'JDictSelectTag',
    componentProps: {
      showSearch: true,
      dictCode: 'goods_unit'
    }
  },
  {
    field: 'minBuy',
    label: '起售',
    defaultValue: 1,
    dynamicDisabled: !userStore.getSelectTenant.isMainTenant,
    helpMessage: '起售数量超出商品库存时，买家无法购买该商品',
    required: true,
    component: 'InputNumber',
    componentProps: {
      size: 'middle',
      style: {
        width: '20%'
      }
    }
  },
  {
    field: 'goodsTypes',
    label: '商品类型',
    required: true,
    defaultValue: [],
    component: 'JDictSelectTag',
    componentProps: {
      mode: 'multiple',
      type: 'list',
      dictCode: 'goods_type',
    }
  },
  {
    label: '规格与库存',
    field: 'specAndStock',
    component: 'Divider',
  },
  {
    field: 'linePrice',
    label: '划线价',
    dynamicDisabled: !userStore.getSelectTenant.isMainTenant,
    helpMessage: '划线价会以划线形式在商品售价旁显示',
    component: 'Input',
    slot: 'linePrice',
  },
  {
    field: 'costPrice',
    label: '成本价',
    slot: 'costPrice',
    dynamicDisabled: !userStore.getSelectTenant.isMainTenant,
    ifShow: ({ values }) => { return !values.multiSpec},
    component: 'Input'
  },

  {
    field: 'bagPrice',
    label: '包装费',
    defaultValue: '0.00',
    dynamicDisabled: !userStore.getSelectTenant.isMainTenant,
    component: 'InputNumber',
    componentProps: {
      addonAfter: '元',
      precision: 1
    }
  },

  {
    field: 'stockAlarm',
    label: '库存告警',
    defaultValue: '0',
    dynamicDisabled: !userStore.getSelectTenant.isMainTenant,
    ifShow: ({ values }) => { return !values.multiSpec},
    component: 'InputNumber'
  },
  {
    field: 'weight',
    label: '商品重量',
    defaultValue: '0.00',
    dynamicDisabled: !userStore.getSelectTenant.isMainTenant,
    ifShow: ({ values }) => { return !values.multiSpec},
    component: 'InputNumber',
    componentProps: {
      addonAfter: 'kg'
    }
  },
  {
    field: 'price',
    label: '商品价格',
    defaultValue: '1',
    dynamicDisabled: !userStore.getSelectTenant.isMainTenant,
    ifShow: ({ values }) => { return !values.multiSpec},
    component: 'InputNumber',
    componentProps: {
      addonAfter: '元',
      precision: 1
    }
  },

  // 单一规格库存管理
  {
    field: 'singleSpecStock',
    label: '库存',
    slot: 'singleSpecStock',
    dynamicDisabled: !userStore.getSelectTenant.isMainTenant,
    ifShow: ({ values }) => { return !values.multiSpec},
    component: 'Input'
  },
  
  // 商品规格数据
  {
    field: 'multiSpec',
    label: '商品规格',
    dynamicDisabled: !userStore.getSelectTenant.isMainTenant,
    defaultValue: false,
    component: 'Input',
    slot: 'spec'
  },
  
  {
    field: 'skuList',
    label: '规格信息',
    slot: 'skuList',
    dynamicDisabled: !userStore.getSelectTenant.isMainTenant,
    ifShow: ({ values }) => { return values.multiSpec === true},
    component: 'Input'
  },
  

  {
    label: '商品属性',
    field: 'attribute',
    component: 'Divider',
  },
  {
    field: 'attributeList',
    label: '商品属性',
    component: 'Input',
    slot: 'attribute'
  },
];


export const specFormSchema: FormSchema[] = [
  {
    field: 'spec',
    label: '规格名称',
    component: 'ApiSelect'
  },
  {
    field: 'specValue',
    label: '规格值',
    component: 'Select',
    componentProps: () => {
      return {
        placeholder: '请选择',
        showSearch: true,
        options: []
      };
    },
  },
]


export const skuStockColumns : JVxeColumn[] = [
  // 表的列id
  { 
    title: 'ID', 
    key: 'id', 
    type: JVxeTypes.hidden 
  },
  { 
    title: '条码', 
    key: 'skuCode', 
    type: JVxeTypes.input, 
    minWidth: 190, 
    defaultValue: '', 
  },
  { 
    title: '零售价(元)', 
    key: 'price', 
    type: JVxeTypes.input, 
    minWidth: 100, 
    // defaultValue: '0.00'
  },
  { 
    title: '成本价(元)', 
    key: 'costPrice', 
    type: JVxeTypes.input, 
    minWidth: 100, 
    // defaultValue: '0.00'
  },
  { 
    title: '实物库存', 
    key: 'stockList', 
    type: JVxeTypes.slot, 
    minWidth: 100, 
    // defaultValue: '0', 
    slotName: 'stockSlot', 
    tipsContent: '此处为所有门店库存之和'
  },
  { 
    title: '库存预警', 
    key: 'stockAlarm', 
    type: JVxeTypes.inputNumber, 
    minWidth: 100, 
    // defaultValue: '5'
  },
  { 
    title: '重量(kg)', 
    key: 'weight', 
    type: JVxeTypes.input,  
    minWidth: 100, 
    // defaultValue: '0.00', 
    tipsContent: '此重量用于第三方配送费计算',
  },
]